package com.atguigu.gulimall.member.web;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.member.feign.OrderFeignService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @create: 2024/9/14
 * @author: gyhe10
 * @description:
 **/

@Controller
public class MemberWebController {

    @Resource
    private OrderFeignService orderFeignService;

    @GetMapping("/memberOrder.html")
    public String memberOrderListPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            Model model,
            ServletRequest request) {
        // 用户支付成功，支付宝的同步通知和异步通知都会携带大量请求数据，包含支付成功的订单号、支付宝流水号、支付金额、验证签名等
        // 当前路径时配置为了支付宝同步通知页面路径，因此可以拿到所有请求参数，然后进行签名验证
        // http://member.gulimall.com/memberOrder.html?charset=utf-8&out_trade_no=202409151225147871835173004477259777&method=alipay.trade.page.pay.return&total_amount=10250.00&sign=ZqEs3jSmmhJJAj6TnwvNPZInYwFHv%2FIOvgCckLPfBJwtkvHToJzty3eNXjpeDDCpgrDkfeTavW8SA2HL2kHk%2BM25t3acmHCA53LhRnrnivxAi%2FH%2FlqVYztyILDBI16duBuKpwqVgwbATjU%2FiDfzB9rahiTFot5J%2BhnhY1OtSu7n5CPQN72OPKSgvGO5K4uAa3%2F7%2F4XYRPphLyywCuxL2xTgib77VO8wYPPbkddb02ZzXDXDDz69dcEz%2BXaHVmorudl21TacbR%2BIlXOxw1zLJsyTy0v%2B4l07XugF3fFtnbE%2F9X5WAN7uIBmHy1uzWioKW%2FxBrpwHJ3Tqq%2B%2FOWCLhznA%3D%3D&trade_no=2024091522001487090504026189&auth_app_id=9021000135658168&version=1.0&app_id=9021000135658168&sign_type=RSA2&seller_id=2088721032525401&timestamp=2024-09-15+12%3A26%3A25
        // 如果验证通过，就认为这个数据是支付宝发送的，然后我们可以根据订单号，然后进行业务处理，比如修改订单状态，修改库存等。
        // Map<String, String[]> parameterMap = request.getParameterMap();

        // 查询当前登录用户的所有订单列表数据
        Map<String, Object> params = new HashMap<>();
        params.put("page", pageNum.toString());
        R r = orderFeignService.getOrderListWithOrderItems(params);
        if (r.getCode() == 0) {
            // System.out.println(JSONUtil.toJsonStr(r));
            model.addAttribute("orders", r);
        }
        return "orderList";
    }
}
